String Functions
are operations provided by the
header to manipulate and analyze strings. These functions enable tasks like finding the length of a string, copying and concatenating strings, comparing strings, and locating characters or substrings within strings. Commonly used string functions include strlen, strcpy, strcat, strcmp, strchr, strstr, and sprintf. These functions facilitate efficient string handling in C programs.
1. strlen()
The strlen() function in C is used to find the length of a null-terminated string. It is part of the
header. The function takes a pointer to the beginning of a string as an argument and returns the number of characters in the string, excluding the null terminator.
Example of strlen() function:
#include <
stdio.h
>
Copy Code
#include <
string.h
>
int
main
() {
char
myString
[] = "
Hello, World!
";
size_t
length
=
strlen
(
myString
);
printf
("
Length of the string: %zu
",
length
);
return
0;
}
Keep in mind that the length does not include the null terminator ' ', and it represents the number of characters in the string before the null terminator.
2. strcpy()
The strcpy() function in C is used to copy the contents of one string to another. It is part of the
header. The function takes two arguments: the destination string and the source string. It copies the characters from the source string to the destination string until it encounters a null terminator (' ') in the source string.
Example of strcpy() function:
#include <
stdio.h
>
Copy Code
#include <
string.h
>
int
main
() {
char
source
[] = "
Hello, World!
";
char
destination
[20];
strcpy
(
destination
,
source
);
printf
("
Source string: %s
",
source
);
printf
("
Destination string: %s
",
destination
);
return
0;
}
Remember, if the destination space isn't big enough for both the source string and the null terminator, it might cause a buffer overflow, leading to unpredictable issues.
3. strcat()
The strcat() function in C is used to concatenate (append) the content of one string to the end of another string. It is part of the
header. The function takes two arguments: the destination string and the source string. It appends the characters from the source string to the end of the destination string and ensures that the resulting string is null-terminated.
Example of strlcat() function:
#include
<
stdio.h
>
Copy Code
#include
<
string.h
>
int
main
() {
char
destination
[20] = "Hello, ";
char
source
[] = "World!";
strcat
(
destination
,
source
);
printf
("
Concatenated string: %s
",
destination
);
return
0;
}
In this example, the strcat() function is used to concatenate the content of the source string to the end of the destination string
4. strncmp()
The strncmp() function in C is used to compare the first n characters of two strings. It is part of the
header. The function takes three arguments: two strings to be compared (str1 and str2) and the number of characters (n) to compare. It returns an integer that indicates the relationship between the two strings.
Example of strncmp() function:
#include
<
stdio.h
>
Copy Code
#include
<
string.h
>
int
main
() {
char
str1
[] = "
apple
";
char
str2
[] = "
apricot
";
int
result
=
strncmp
(
str1
,
str2
,
3
);
if
(
result
< 0) {
printf
("
str1 is less than str2
");
}
else if
(
result
> 0) {
printf
("
str1 is greater than str2
");
}
else
{
printf
("
str1 is equal to str2
");
}
return
0;
}
In this example, the strncmp() function is used to compare the first 3 characters of the strings str1 and str2. The result is then checked, and a message is printed based on the comparison.